package com.example.qu_tao.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.example.qu_tao.database.DBHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TQProvider extends ContentProvider {
    private static final String AUTHORITY = "com.example.qu_tao.provider";
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int MATCHER_GOODS = 1;
    private static final int MATCHER_GOODS_ID = 2;
    private static final int MATCHER_USER = 11;
    private static final int MATCHER_USER_ID = 12;
    private static final int MATCHER_PHOTO = 21;
    private static final int MATCHER_POSTS = 31;
    private static final int MATCHER_POSTS_ID = 32;
    private static final int MATCHER_COLLECTION = 41;
    private static final int MATCHER_COLLECTION_ID = 42;
    private static final int MATCHER_CART = 51;
    private static final int MATCHER_CART_ID = 52;
    private static final int MATCHER_ORDER = 61;
    private static final int MATCHER_ORDER_ID = 62;
    private static final int MATCHER_NEWS = 71;
    private static final int MATCHER_NEWS_ID = 72;
    private DBHelper mDBHelper;

    static {//通配符：*所有字符，#所有数字
        // content://com.jn.zhinongbao.provider/goods
        uriMatcher.addURI(AUTHORITY, "goods", MATCHER_GOODS);
        // content://com.jn.zhinongbao.provider/goods/1
        uriMatcher.addURI(AUTHORITY, "goods/#", MATCHER_GOODS_ID);
        uriMatcher.addURI(AUTHORITY, "user", MATCHER_USER);
        uriMatcher.addURI(AUTHORITY, "user/#", MATCHER_USER_ID);
        uriMatcher.addURI(AUTHORITY, "photo", MATCHER_PHOTO);
        uriMatcher.addURI(AUTHORITY, "posts", MATCHER_POSTS);
        uriMatcher.addURI(AUTHORITY, "posts/#", MATCHER_POSTS_ID);
        uriMatcher.addURI(AUTHORITY, "collection", MATCHER_COLLECTION);
        uriMatcher.addURI(AUTHORITY, "collection/#", MATCHER_COLLECTION_ID);
        uriMatcher.addURI(AUTHORITY, "cart", MATCHER_CART);
        uriMatcher.addURI(AUTHORITY, "cart/#", MATCHER_CART_ID);
        uriMatcher.addURI(AUTHORITY, "order1", MATCHER_ORDER);
        uriMatcher.addURI(AUTHORITY, "order1/#", MATCHER_ORDER_ID);
        uriMatcher.addURI(AUTHORITY, "news", MATCHER_NEWS);
        uriMatcher.addURI(AUTHORITY, "news/#", MATCHER_NEWS_ID);
    }

    //运行一次加载数据库
    @Override
    public boolean onCreate() {
        mDBHelper = new DBHelper(getContext());
        return false;
    }

    @androidx.annotation.Nullable
    @Override
    public Cursor query(@androidx.annotation.NonNull Uri uri,
                        @androidx.annotation.Nullable String[] projection,
                        @androidx.annotation.Nullable String selection,
                        @androidx.annotation.Nullable String[] selectionArgs,
                        @androidx.annotation.Nullable String sortOrder) {
        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:
                return db.query("goods", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);
                return db.query("goods", projection, "goods_id = ?",
                        new String[]{String.valueOf(goods_id)}, null,
                        null, sortOrder, null);
            case MATCHER_USER:
                return db.query("user", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_USER_ID:
                long user_id = ContentUris.parseId(uri);
                Log.d("provider", "user_id ========= " + user_id);
                return db.query("user", projection, "_id = ?",
                        new String[]{String.valueOf(user_id)}, null,
                        null, sortOrder, null);
            case MATCHER_PHOTO:
                return db.query("photo", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_POSTS:
                return db.query("posts", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_POSTS_ID:
                long post_id = ContentUris.parseId(uri);
                return db.query("posts", projection, "post_id = ?",
                        new String[]{String.valueOf(post_id)}, null, null, sortOrder);
            case MATCHER_COLLECTION:
                return db.query("collection", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_COLLECTION_ID:
                long collection_id = ContentUris.parseId(uri);
                return db.query("collection", projection, "collection_id = ?",
                        new String[]{String.valueOf(collection_id)}, null, null, sortOrder);
            case MATCHER_CART:
                return db.query("cart", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_CART_ID:
                long cart_id = ContentUris.parseId(uri);
                return db.query("cart", projection, "cart_id = ?",
                        new String[]{String.valueOf(cart_id)}, null,
                        null, sortOrder, null);
            case MATCHER_ORDER:
                return db.query("order1", projection, selection, selectionArgs, null, null, sortOrder);
            case MATCHER_ORDER_ID:
                long order_id = ContentUris.parseId(uri);
                return db.query("order1", projection, "order_id = ?",
                        new String[]{String.valueOf(order_id)}, null, null, sortOrder);
            case MATCHER_NEWS:
                return db.query("news", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_NEWS_ID:
                long news_id = ContentUris.parseId(uri);
                return db.query("news", projection, "news_id = ?",
                        new String[]{String.valueOf(news_id)}, null,
                        null, sortOrder);
            default:
                return null;
        }
    }

    @androidx.annotation.Nullable
    @Override
    public String getType(@androidx.annotation.NonNull Uri uri) {
        return null;
    }

    @androidx.annotation.Nullable
    @Override
    public Uri insert(@androidx.annotation.NonNull Uri uri, @androidx.annotation.Nullable ContentValues values) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        long insertId = -1;
        String tableName = null;
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:
                insertId = db.insert("goods", null, values);
                break;
            case MATCHER_USER:
                insertId = db.insert("user", null, values);
                break;
            case MATCHER_PHOTO:
                insertId = db.insert("photo", null, values);
                break;
            case MATCHER_POSTS:
                tableName = "posts";
                values.put("create_time", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                break;
            case MATCHER_COLLECTION:
                tableName = "collection";
                break;
            case MATCHER_CART:
                tableName = "cart";
                break;
            case MATCHER_ORDER:
                tableName = "order1";
                break;
            case MATCHER_NEWS:
                tableName = "news";
                break;
        }
        if (tableName != null) {
            insertId = db.insert(tableName, null, values);
        }
        if (insertId != -1) {
            return ContentUris.withAppendedId(uri, insertId);
        }
        return null;
    }

    //大量插入
    @Override
    public int bulkInsert(@androidx.annotation.NonNull Uri uri, @androidx.annotation.NonNull ContentValues[] values) {
        int count = 0;
        for (ContentValues contentValues : values) {
            Uri insertedUri = insert(uri, contentValues);
            if (insertedUri != null) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int delete(@androidx.annotation.NonNull Uri uri, @androidx.annotation.Nullable String selection, @androidx.annotation.Nullable String[] selectionArgs) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        int rowsDeleted = 0;
        String tableName = null;
        String whereClause = null;
        String[] whereArgs = null;
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:
                rowsDeleted = db.delete("goods", selection, selectionArgs);
                break;
            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);
                rowsDeleted = db.delete("goods", "goods_id = ?", new String[]{String.valueOf(goods_id)});
                break;
            case MATCHER_USER:
                rowsDeleted = db.delete("user", selection, selectionArgs);
                break;
            case MATCHER_USER_ID:
                long user_id = ContentUris.parseId(uri);
                rowsDeleted = db.delete("user", "_id = ?", new String[]{String.valueOf(user_id)});
                break;
            case MATCHER_PHOTO:
                rowsDeleted = db.delete("photo", selection, selectionArgs);
                break;
            case MATCHER_POSTS:
                tableName = "posts";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_POSTS_ID:
                tableName ="posts";
                long post_id = ContentUris.parseId(uri);
                whereClause = "post_id = ?";
                whereArgs = new String[]{String.valueOf(post_id)};
                break;
            case MATCHER_COLLECTION:
                tableName = "collection";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_COLLECTION_ID:
                tableName = "collection";
                long collection_id = ContentUris.parseId(uri);
                whereClause = "collection_id = ?";
                whereArgs = new String[]{String.valueOf(collection_id)};
                break;
            case MATCHER_CART:
                tableName = "cart";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_CART_ID:
                tableName = "cart";
                long cart_id = ContentUris.parseId(uri);
                whereClause = "cart_id = ?";
                whereArgs = new String[]{String.valueOf(cart_id)};
                break;
            case MATCHER_ORDER:
                tableName = "order1";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_ORDER_ID:
                tableName = "order1";
                long order_id = ContentUris.parseId(uri);
                whereClause = "order_id = ?";
                whereArgs = new String[]{String.valueOf(order_id)};
                break;
            case MATCHER_NEWS:
                tableName = "news";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_NEWS_ID:
                tableName = "news";
                long news_id = ContentUris.parseId(uri);
                whereClause = "news_id = ?";
                whereArgs = new String[]{String.valueOf(news_id)};
                break;

        }
        if (tableName != null) {
            rowsDeleted = db.delete(tableName, whereClause, whereArgs);
        }
        return rowsDeleted;
    }

    @Override
    public int update(@androidx.annotation.NonNull Uri uri, @androidx.annotation.Nullable ContentValues values, @androidx.annotation.Nullable String selection, @androidx.annotation.Nullable String[] selectionArgs) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        int rowsUpdated = 0;
        String tableName = null;
        String whereClause = null;
        String[] whereArgs = null;
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:
                rowsUpdated = db.update("goods", values, selection, selectionArgs);
                break;
            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);
                rowsUpdated = db.update("goods", values, "goods_id = ?", new String[]{String.valueOf(goods_id)});
                break;
            case MATCHER_USER:
                rowsUpdated = db.update("user", values, selection, selectionArgs);
                break;
            case MATCHER_USER_ID:
                long user_id = ContentUris.parseId(uri);
                rowsUpdated = db.update("user", values, "_id = ?", new String[]{String.valueOf(user_id)});
                break;
            case MATCHER_PHOTO:
                rowsUpdated = db.update("photo", values, selection, selectionArgs);
                break;
            case MATCHER_POSTS:
                tableName = "posts";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_POSTS_ID:
                tableName ="posts";
                long post_id = ContentUris.parseId(uri);
                whereClause = "post_id = ?";
                whereArgs = new String[]{String.valueOf(post_id)};
                break;
            case MATCHER_COLLECTION:
                tableName = "collection";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_COLLECTION_ID:
                tableName = "collection";
                long collection_id = ContentUris.parseId(uri);
                whereClause = "collection_id = ?";
                whereArgs = new String[]{String.valueOf(collection_id)};
                break;
            case MATCHER_CART:
                tableName = "cart";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_CART_ID:
                tableName = "cart";
                long cart_id = ContentUris.parseId(uri);
                whereClause = "cart_id = ?";
                whereArgs = new String[]{String.valueOf(cart_id)};
                break;
            case MATCHER_ORDER:
                tableName = "order1";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_ORDER_ID:
                tableName = "order1";
                long order_id = ContentUris.parseId(uri);
                whereClause = "order_id = ?";
                whereArgs = new String[]{String.valueOf(order_id)};
                break;
            case MATCHER_NEWS:
                tableName = "news";
                whereClause = selection;
                whereArgs = selectionArgs;
                break;
            case MATCHER_NEWS_ID:
                tableName = "news";
                long news_id = ContentUris.parseId(uri);
                whereClause = "news_id = ?";
                whereArgs = new String[]{String.valueOf(news_id)};
                break;
        }
        if (tableName != null) {
            rowsUpdated = db.update(tableName, values, whereClause, whereArgs);
        }
        return rowsUpdated;
    }
}